package com.ztgf.shopping.api.cms.epg;

import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.shopping.models.bo.epg.EpgTopicBO;
import com.ztgf.shopping.models.bo.epg.EpgTopicSaveBO;
import com.ztgf.shopping.models.dto.epg.EpgTopicDTO;
import com.ztgf.shopping.models.service.epg.IEpgTopicService;
import com.ztgf.shopping.models.vo.epg.EpgTopicVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * author: chenyu.wang
 * date: 2020-05-20 14:23
 * Description:
 */
@RestController
@RequestMapping("/cms/epgTopic")
@Slf4j
@Api(value = "EpgTopicController", tags = "专题(推荐专区)接口")
public class EpgTopicController {

    @DubboReference
    private IEpgTopicService topicService;

    /**
     * 专题(推荐专区) 根据功能id
     * @param moduleId 功能id
     * @return
     */
    @ApiOperation(value = "专题(推荐专区)列表 根据功能id",notes = "专题(推荐专区)列表 根据功能id")
    @PostMapping("/getListByModuleId")
    public Result<List<EpgTopicVO>> getListByModuleId(Long moduleId){
        EpgTopicBO epgTopicBO = new EpgTopicBO();
        epgTopicBO.setModuleId(moduleId);
        List<EpgTopicVO> epgTopicVOList = topicService.getBySelectTopicInfoNoPage(epgTopicBO);
        return new Result<>(epgTopicVOList);
    }

    @PostMapping(value = "/insert")
    @ApiOperation(value = "添加专题(推荐专区)表", notes = "添加专题(推荐专区)表")
    public Result insert( EpgTopicSaveBO topicSaveBO) {
        boolean flag = topicService.add(topicSaveBO);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改专题(推荐专区)表", notes = "修改专题(推荐专区)表")
    public Result update( EpgTopicSaveBO topicSaveBO) {
        EpgTopicDTO epgTopicDTO = new EpgTopicDTO();
        BeanUtils.copyProperties(topicSaveBO, epgTopicDTO);
        epgTopicDTO.setId(topicSaveBO.getId());
        boolean flag = topicService.updateById(epgTopicDTO);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除专题(推荐专区)表", notes = "删除专题(推荐专区)表")
    @ApiImplicitParam(name = "id", value = "字典表id", required = true, paramType = "query", dataType = "Long")
    public Result delete(@RequestParam Long id) {
        boolean flag = topicService.deleteById(id);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除专题(推荐专区)表", notes = "批量删除专题(推荐专区)表")
    @ApiImplicitParam(name = "ids", value = "ids:以逗号隔开", required = true, paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam String ids) {
        if (StrUtils.isEmpty(ids)) {
            return new Result(CodeEnum.FAIL_100002);
        }
        String[] arr = ids.split(",");
        topicService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/getById")
    @ApiOperation(value = "根据id查找", notes = "根据id查找")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result<EpgTopicDTO> getById(@RequestParam Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        return new Result<>(topicService.selectById(id));
    }

    @PostMapping("/querySelectPageList")
    @ApiOperation(value = "专题(推荐专区)列表", notes = "专题(推荐专区)列表")
    public Result<Page<EpgTopicVO>> querySelectPageList(EpgTopicBO topicBO) {
        if(null == topicBO)
            return new Result<>(CodeEnum.FAIL_100002 );
        Page<EpgTopicVO> page = topicService.getBySelectTopicInfoPage(topicBO);
        return new Result<>(page);
    }
}
